package com.github.skystardust.lotteryex.lottery.item.imp.require;

import com.github.skystardust.lotteryex.LotteryEX;
import com.github.skystardust.lotteryex.hooks.PlayerPointsHook;
import com.github.skystardust.lotteryex.lottery.item.RequireItem;
import com.github.skystardust.lotteryex.utils.OptionalUtils;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class RequireItemPoints extends RequireItem<Integer> {
    public RequireItemPoints(Integer requireObject) {
        super(requireObject);
    }

    public RequireItemPoints(UUID uuid, Integer requireObject) {
        super(uuid, requireObject);
    }

    public RequireItemPoints(Map<String, Object> stringObjectMap) {
        super(stringObjectMap);
        setRequireObject(((Integer) stringObjectMap.get("requireObject")));
    }

    @Override
    public boolean hasRequireObject(Player player) {
        return new OptionalUtils<>(() -> LotteryEX.getLotteryEX().getPlayerPointsHook())
                .getOptional()
                .map(PlayerPointsHook::getPlayerPointsAPI)
                .map(playerPointsAPI -> playerPointsAPI.look(player.getUniqueId()) >= getRequireObject())
                .orElse(false);
    }

    @Override
    public void takeItem(Player player) {
        new OptionalUtils<>(() -> LotteryEX.getLotteryEX().getPlayerPointsHook())
                .getOptional()
                .ifPresent(playerPointsHook -> {
                    playerPointsHook.getPlayerPointsAPI().take(player.getUniqueId(), getRequireObject());
                });
    }
}
